package com.adhere.system.utils;

import org.hashids.Hashids;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class CodeUtils {

    // 盐不要随意改
    private static final String SALT = "HashidsSalt:2f526979-eb1f-498d-8187-c1362ccada81";
    private static final Integer MIN_HASH_LENGTH = 11;
    private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-";

    public static String encode(Long num) {
        return new Hashids(SALT, MIN_HASH_LENGTH, ALPHABET).encode(num);
    }

    public static String encode(long[] longArray) {
        return new Hashids(SALT, MIN_HASH_LENGTH).encode(longArray);
    }

    public static List<Long> decodeArray(String code) {
        long[] longArray = new Hashids(SALT, MIN_HASH_LENGTH).decode(code);
        //这边是 long[] 使用 List<long[]> longs = Arrays.asList(longArray); 注意它的返回值 只有Long[] 的才可以
        return Arrays.stream(longArray).boxed().collect(Collectors.toList());
    }

}
